﻿using PhotoGallery.Data;
using PhotoGallery.Data.Entity;
using PhotoGallery.Data.Repository;
using PhotoGallery.Infrastructure;
using PhotoGallery.Models.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;

namespace PhotoGallery.Models
{
    public class AudioInfo
    {
        public int ID { get; set; }
        [StringLength(25, ErrorMessageResourceType = typeof(ErrorMessages),
            ErrorMessageResourceName = "nameIsTooLong")]
        [LocalizedDisplayName("title", NameResourceType = typeof(Names))]
        public string Title { get; set; }
        public string FileName { get; set; }
        public string Thumbnail { get; set; }
        [DataType(DataType.MultilineText)]
        [LocalizedDisplayName("description", NameResourceType = typeof(Names))]
        public string Description { get; set; }
        [LocalizedDisplayName("date", NameResourceType = typeof(Names))]
        [DataType(DataType.Date)]
        public DateTime? UploadDate { get; set; }

        public string Url { get; set; }
        public string UrlThumbnail { get; set; }
        public string FullFileName { get; set; }
        public string FullThumbnailName { get; set; }
    }

    public class AudioModel : BaseModel
    {
        string path, url;
        Album album;
        private AudioRepository repository;

        public AudioModel(int albumID)
        {
            album = new AlbumRepository(Owner).Get(albumID);
            path = BasePath + Settings.UploadsFolder + Owner + "\\Audio\\" + getAlbumFolder(album);
            url = Settings.UploadsUrl + Owner + "/Audio/" + getAlbumUrl(album);

            // TODO var albumEntity = AlbumsModel.Convert(album);
            repository = new AudioRepository(album, Owner);
        }

        public AudioModel(int albumID, string owner, string albumFolder)
        {
            album = new AlbumRepository(Owner).Get(albumID);
            path = BasePath + Settings.UploadsFolder + owner + "\\Audio\\" + albumFolder+"\\";
            url = Settings.UploadsUrl + owner + "/Audio/" + albumFolder+"/";
            repository = new AudioRepository(albumID, owner, albumFolder);
        }

        public IEnumerable<AudioInfo> Get()
        {
            List<AudioInfo> audio = new List<AudioInfo>();
            var data = repository.Get();
            foreach (var entity in data)
            {
                var item = new AudioInfo()
                {
                    ID = entity.ID,
                    Title = entity.Title,
                    FileName = entity.FileName,
                    Description = entity.Description,
                    Thumbnail = entity.Thumbnail,

                    Url = url + entity.FileName,
                    UrlThumbnail = url + entity.Thumbnail,
                    FullFileName = path + entity.FileName,
                    FullThumbnailName = path + entity.Thumbnail
                };
                audio.Add(item);
            }
            return audio;
        }

        public AudioInfo Get(int id)
        {
            var entity = repository.Get(id);
            if (entity != null)
            {
                var item = new AudioInfo()
                {
                    ID = entity.ID,
                    Title = entity.Title,
                    Description = entity.Description,
                    FileName = entity.FileName,
                    Thumbnail = entity.Thumbnail,
                    UploadDate = entity.UploadedOn,

                    Url = url + entity.FileName,
                    UrlThumbnail = url + entity.Thumbnail,
                    FullFileName = path + entity.FileName,
                    FullThumbnailName = path + entity.Thumbnail
                };
                return item;
            }
            return null;
        }

        public AudioInfo GetFirst()
        {
            var entity = repository.GetFirst();
            if (entity != null)
            {
                var item = new AudioInfo()
                {
                    ID = entity.ID,
                    Title = entity.Title,
                    Description = entity.Description,
                    FileName = entity.FileName,
                    Thumbnail = entity.Thumbnail,
                    UploadDate = entity.UploadedOn,

                    Url = url + entity.FileName,
                    FullFileName = path + entity.FileName,
                    FullThumbnailName = path + entity.Thumbnail
                };
                return item;
            }
            else
            {
                return null;
            }

        }

        public void Create(AudioInfo audio, HttpPostedFileBase file, HttpPostedFileBase thummnail)
        {
            if (file == null || file.FileName == string.Empty)
            {
                return;
            }
            audio.FileName = FileUtils.CheckFileExists(file, path);
            if (thummnail != null)
            {
                string tmpThummnailName = file.FileName;
                tmpThummnailName = Path.ChangeExtension(tmpThummnailName, Path.GetExtension(thummnail.FileName));
                audio.Thumbnail = FileUtils.CheckFileExists(thummnail, path, tmpThummnailName);
                thummnail.SaveAs(path + audio.Thumbnail);
                // thumbnail
                string resizedFileName = "thump-" + audio.Thumbnail;
                FileHelper.SaveResizedImage(path, audio.Thumbnail,  resizedFileName);
                File.Delete(path + audio.Thumbnail);
                audio.Thumbnail = resizedFileName;
            
            }
            if (string.IsNullOrEmpty(audio.Thumbnail))
            {
                audio.Thumbnail = "";
            }
            file.SaveAs(path + audio.FileName);

            Audio entity = Convert(audio);
            repository.Create(entity);
        }

        public void Update(AudioInfo audio, HttpPostedFileBase file, HttpPostedFileBase thummnail)
        {
            if (file != null)
            {
                audio.FileName = FileUtils.CheckFileExists(file, path);
                file.SaveAs(path + audio.FileName);
            }
            if (thummnail != null)
            {
                string tmpThummnailName = thummnail.FileName;
                tmpThummnailName = Path.ChangeExtension(tmpThummnailName, Path.GetExtension(thummnail.FileName));
                audio.Thumbnail = FileUtils.CheckFileExists(thummnail, path, tmpThummnailName);
                thummnail.SaveAs(path + audio.Thumbnail);
            }

            Audio entity = Convert(audio);
            repository.Update(entity);
        }

        public void Delete(AudioInfo audio)
        {
            Audio entity = Convert(audio);
            repository.Delete(entity);
        }

        private static Audio Convert(AudioInfo audio)
        {
            Audio entity = new Audio()
            {
                ID = audio.ID,
                Title = audio.Title,
                Description = audio.Description,
                FileName = audio.FileName,
                Thumbnail = audio.Thumbnail,
                UploadedOn = audio.UploadDate
            };
            return entity;
        }

        public void Erise(AudioInfo audio)
        {
            if (audio.FileName != string.Empty && System.IO.File.Exists(path + audio.FileName))
            {
                System.IO.File.Delete(path + audio.FileName);
            }
            if (audio.Thumbnail != string.Empty && System.IO.File.Exists(path + audio.Thumbnail))
            {
                System.IO.File.Delete(path + audio.Thumbnail);
            }
            repository.Erise(audio.ID);
        }
    }
}